/*
 * This file is part of ELCube.
 *
 * ELCube is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ELCube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
 */
package cn.nkpro.elcube.platform.controller;

import cn.nkpro.elcube.annotation.NkNote;
import cn.nkpro.elcube.co.quartz.NkSchedulerManager;
import cn.nkpro.elcube.co.quartz.model.NkJobDetail;
import cn.nkpro.elcube.co.quartz.model.TriggerV;
import cn.nkpro.elcube.utils.BeanUtilz;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by bean on 2022/6/16.
 */
@NkNote("25.[DevOps]计划管理")
@RestController
@RequestMapping("/ops/scheduled")
@PreAuthorize("hasAnyAuthority('*:*','DEVOPS:*','DEVOPS:SCHEDULED')")
public class DevOpsScheduledController {

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private NkSchedulerManager schedulerManager;

    @NkNote("1.获取任务列表")
    @RequestMapping(value = "/jobs")
    public List<NkJobDetail> jobs() throws SchedulerException {
        return schedulerManager.jobs();
    }

    @NkNote("2.获取任务详情")
    @RequestMapping(value = "/job/{group}/{name}")
    public NkJobDetail job(@PathVariable String name, @PathVariable String group) throws SchedulerException {
        NkJobDetail jobDetail = new NkJobDetail();
        jobDetail.setTriggers(scheduler.getTriggersOfJob(JobKey.jobKey(name,group))
                    .stream()
                    .map(t-> BeanUtilz.copyFromObject(t, TriggerV.class))
                    .collect(Collectors.toList()));
        return jobDetail;
    }

    @NkNote("3.立即执行任务")
    @RequestMapping(value = "/execute/{group}/{name}")
    public void execute(@PathVariable String name, @PathVariable String group, @RequestBody(required=false) String options) throws SchedulerException {
        JobDataMap dataMap = new JobDataMap();
        dataMap.put("options",options);
        scheduler.triggerJob(JobKey.jobKey(name,group),dataMap);
    }

    @RequestMapping(value = "/trigger/add/{group}/{name}")
    public void addTrigger(@PathVariable String name, @PathVariable String group, @RequestParam("name") String triggerName, @RequestParam("cron")String triggerCron) throws SchedulerException {
        schedulerManager.addTrigger(name, group, triggerName, triggerCron);
    }

    @RequestMapping(value = "/trigger/remove")
    public void removeTrigger(@RequestParam("name") String triggerName,@RequestParam("group") String triggerGroup) throws SchedulerException {
        schedulerManager.removeTrigger(triggerName, triggerGroup);
    }
}
